import type {SysRoleData, SysRoleAdd} from "@/pages/system/role/type";
import type {ReactNode} from "react";
import type {UseFetchDataAction} from "@ant-design/pro-table/es/typing";
import {Button, Space, Tag} from "antd";
import type {ProColumns} from "@ant-design/pro-table";
import type {OptionActions} from "@/type/type";
import AddSysRole from "@/pages/system/role/list/components/AddSysRole";


// @ts-ignore
const columns: ProColumns<SysRoleData> = (optionAction: OptionActions<SysRoleAdd, SysRoleData>) => {
  return [
    {
      dataIndex: "index",
      valueType: "index",
    },
    {
      title: "角色名称",
      dataIndex: "roleName",
      align: 'center',
    },
    {
      title: "颜色",
      dataIndex: "color",
      align: 'center',
      hideInSearch: true,
      render: (text: ReactNode, record: SysRoleData, index: number, action: UseFetchDataAction<SysRoleData>) => {
        return <Tag color={record.color} style={{height: 20, width: 30}} bordered={false}></Tag>
      }
    },
    {
      title: "角色状态",
      dataIndex: "status",
      align: 'center',
      hideInSearch: true,
      render: (text: ReactNode, record: SysRoleData, index: number, action: UseFetchDataAction<SysRoleData>) => {
        const editStatus = (record: SysRoleData) => {
          optionAction.edit({
            ...record,
            status: !record.status,
            menuIds: record.roleMenuList.map(item => item.menuId)
          })
        }
        return record.status ?
          (<Tag color="green" onClick={() => editStatus(record)}>启用</Tag>) :
          (<Tag color="yellow" onClick={() => editStatus(record)}>停用</Tag>)
      }
    },
    {
      title: "操作",
      valueType: 'option',
      align: "center",
      key: "option",
      width: "200px",
      render: (text: ReactNode, record: SysRoleData, index: number, action: UseFetchDataAction<SysRoleData>) => {
        return (
          <Space>
            <AddSysRole initialValues={record} {...optionAction.editModal(record)}/>
            <Button type="link" danger onClick={() => optionAction.remove([record.id])}>删除</Button>
          </Space>
        )
      }
    }
  ]


}


export default columns;
